package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dto.SKUPresentDTO;
import mobi.hami.huapin.model.UserOrder;
import mobi.hami.huapin.model.UserOrderDetail;
import mobi.hami.huapin.model.UserPresentSku;
import mobi.hami.huapin.service.HauPinTaskService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Create by 小峰 on 2018/9/26.
 */
@Service
public class HauPinTaskServiceImpl implements HauPinTaskService {
    /**
     * 计算出赠送的商品
     *
     * @param skuPreSentList   赠送商品的列表
     * @param presentCountList 已经送出的商品
     * @param huapinSkuId      拼团已经送出的商品
     * @return
     */
    public SKUPresentDTO calculatePresentSKU(List<SKUPresentDTO> skuPreSentList, List<UserPresentSku> presentCountList, List<Integer> huapinSkuId) {

        List<SKUPresentDTO> filterSku = skuPreSentList;
        SKUPresentDTO skuPresentDTO;
        if (presentCountList.size() > 0) {
            Integer mycount = presentCountList.get(0).getMycount();
            if (mycount > 1) {
                presentCountList = presentCountList.stream().filter(m -> m.getMycount().equals(mycount)).collect(Collectors.toList());
            }
            if (presentCountList != null && presentCountList.size() > 0) {
                List<Integer> skuList = presentCountList.stream().map(m -> m.getSku_id()).collect(Collectors.toList());
                filterSku = skuPreSentList.stream().filter(m -> !skuList.contains(m.getId())).collect(Collectors.toList());
            }
            if (filterSku == null || filterSku.size() <= 0) {
                filterSku = skuPreSentList;
            }
        }
        if (huapinSkuId != null && huapinSkuId.size() > 0 && filterSku.size() > 1) {
            List<SKUPresentDTO> HuaPinfilterSku = filterSku.stream().filter(m -> !huapinSkuId.contains(m.getId())).collect(Collectors.toList());
            if (HuaPinfilterSku != null && HuaPinfilterSku.size() > 0) {
                filterSku = HuaPinfilterSku;
            }
        }
        if (filterSku == null || filterSku.size() <= 0) {
            filterSku = skuPreSentList;
        }
        if (filterSku.size() == 1) {
            return filterSku.get(0);
        } else {
            Random random = new Random();
            int r = random.nextInt(filterSku.size()); //获得随机下标
            return filterSku.get(r);
        }

    }




}
